﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZXing;  //生成二维码的命名空间

namespace MyTest
{
    /// <summary>
    /// 教员:薛海
    /// 微信:xuehai03
    /// QQ:289477003
    /// 二维码登录
    /// 2019-2-20
    /// </summary>
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }
        //随机数
        Random random = new Random();
        public string num;

        //点击生成二维码
        private void btnStart_Click(object sender, EventArgs e)
        {
            //生成6位随机数
            num = random.Next(1, 1000000).ToString();
            getImg(num);
        }

        //生成二维码的方法
        public Bitmap getImg(string msg)
        {
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "utf-8");
            writer.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
            const int codeSize = 140;
            writer.Options.Height = writer.Options.Width = codeSize;
            writer.Options.Margin = 1;
            ZXing.Common.BitMatrix bm = writer.Encode(msg);
            Bitmap img = writer.Write(bm);
            this.pictureBox1.Image = img;
            return img;
        }

        //登录操作
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(this.txtNum.Text))
            {
                MessageBox.Show("请扫描二维码并输入数字");
                return;
            }
            if (num.Equals(this.txtNum.Text))
            {
               // MessageBox.Show("登录成功");
                FrmMain fm = new FrmMain();
                fm.Show();
            }
            this.Hide();
            
        }
    }
}
